
package com.gopay.bis.citic.fund.operation;

import org.apache.log4j.Logger;

import java.io.*;

/**
 * @KeyIO.java
 * @author dong-wang5@hnari.com
 * @2016年7月20日 上午10:02:02  www.gopay.com.cn Inc.All rights reserved.
 */
public class KeyIO{

    private static Logger logger = Logger.getLogger(KeyIO.class);

    /**
     *
     * @param file
     * @return
     * @throws java.io.IOException
     */
    public static String readKeyString(File file)
    {
        FileInputStream fileInputStream = null;
        InputStream ips = null;
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer("");
        try {
            fileInputStream = new FileInputStream(file);
            ips = new BufferedInputStream(fileInputStream);
            br = new BufferedReader(new InputStreamReader(ips));

            String lineContent = br.readLine();
            while (null != lineContent)
            {
                sb.append(lineContent);
                sb.append("\r\n");
                lineContent = br.readLine();
            }
        } catch (FileNotFoundException e) {
            logger.error("=====>验签证书不存在"+e.getMessage());
        } catch (IOException e) {
            logger.error("=====>验签证书获取失败" + e.getMessage());
        } finally {
            try {
                if (br!=null){
                    br.close();
                }
                if (ips != null) {
                    ips.close();
                }
                if (fileInputStream !=null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                logger.error("=====>验签证书流关闭失败"+e.getMessage());
            }
        }
        return new String(sb);
    }
//    public static String readKeyString(InputStream fis) throws IOException
//    {
//        InputStream ips = new BufferedInputStream(fis);
//        BufferedReader br = new BufferedReader(new InputStreamReader(ips));
//        StringBuffer sb = new StringBuffer("");
//        String lineContent = br.readLine();
//        while (null != lineContent)
//        {
//            sb.append(lineContent);
//            sb.append("\r\n");
//            lineContent = br.readLine();
//        }
//        ips.close();
//        return new String(sb);
//    }
    /**
     * <pre>
     * ����Կ�ַ�д���ļ�
     * </pre>
     *
     * @param file
     * @param keyString
     * @throws java.io.IOException
     */
    public static void writeKeyString(File file, String keyString) throws IOException 
    {
        Writer writer = new BufferedWriter(new FileWriter(file));
        writer.write(keyString);
        writer.flush();
        writer.close();
    }

    /**
     * <pre>
     * ���ļ��ж�ȡ��Կ�ֽ�����
     * </pre>
     *
     * @param file
     * @return
     * @throws java.io.IOException
     */
    public static byte[] readKeyBytes(File file) throws IOException 
    {
        int bufferSize = 1024;
        InputStream input = new BufferedInputStream(new FileInputStream(file));
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        byte[] temp = new byte[bufferSize];
        while (input.read(temp) != -1) 
        {
            output.write(temp);
        }
        byte[] array = output.toByteArray();
        output.close();
        input.close();
        return array;
    }

    /**
     * <pre>
     * ����Կ�ֽ�����д���ļ�
     * </pre>
     *
     * @param file
     * @param keyBytes
     * @throws java.io.IOException
     */
    public static void writeKeyBytes(File file, byte[] keyBytes) throws IOException 
    {
        OutputStream opt = new BufferedOutputStream(new FileOutputStream(file));
        opt.write(keyBytes);
        opt.flush();
        opt.close();
    }
}
